package csel.model;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class TileTester extends TestCase{
	RealTile realTile;
	RealTile[][] tiles;
	Map map;
	public static void main(String args[]) 
	{ 
		junit.textui.TestRunner.run(MapTester.class); 
	} 
	
	@Before
	protected void setUp() throws Exception
	{
		realTile = new RealTile();
		tiles = new RealTile[100][100];
		for(int i = 0; i < 100; i++)
		{
			for(int j = 0; j<100; j++)
			{
				tiles[i][j] = new RealTile();
				tiles[i][j].setLocation(new Location(i, j));
			}
		}
		map = new Map(tiles);
		realTile.setMap(map);
		realTile.setLocation(new Location (3,3));
	}
	
	@Test
	public void testMapWithTiles()
	{
		RealTile[][] test = (RealTile[][]) map.getTileMatrix();
		for(int i = 0; i < 100; i++)
		{
			for(int j = 0; j<100; j++)
			{
				Assert.assertEquals(tiles[i][j], test[i][j]);
			}
		}
		tiles[3][3] = realTile;
		map.setTileMatrix(tiles);		
		
	}
	
	@Test
	//test realTile's functions
	public void testRealTiles()
	{
		Location location = realTile.getNeighbor(Direction.NORTH).getLocation();
		Assert.assertEquals(location.getX(), 3);
		Assert.assertEquals(location.getY(), 2);
		
		location = realTile.getNeighbor(Direction.NORTHWEST).getLocation();
		Assert.assertEquals(location.getX(), 4);
		Assert.assertEquals(location.getY(), 2);
		
		location = realTile.getNeighbor(Direction.NORTHEAST).getLocation();
		Assert.assertEquals(location.getX(), 2);
		Assert.assertEquals(location.getY(), 2);
		
		location = realTile.getNeighbor(Direction.SOUTHWEST).getLocation();
		Assert.assertEquals(location.getX(), 4);
		Assert.assertEquals(location.getY(), 4);
		
		location = realTile.getNeighbor(Direction.SOUTHEAST).getLocation();
		Assert.assertEquals(location.getX(), 2);
		Assert.assertEquals(location.getY(), 4);
		
		location = realTile.getNeighbor(Direction.SOUTH).getLocation();
		Assert.assertEquals(location.getX(), 3);
		Assert.assertEquals(location.getY(), 4);
		
		location = realTile.getNeighbor(Direction.EAST).getLocation();
		Assert.assertEquals(location.getX(), 2);
		Assert.assertEquals(location.getY(), 3);
		
		location = realTile.getNeighbor(Direction.NORTHWEST).getLocation();
		Assert.assertEquals(location.getX(), 4);
		Assert.assertEquals(location.getY(), 2);
	}
	
	@Test
	//test realTile's functions
	public void testEnter()
	{
		Entity e = new Entity(new Summoner());
		RealTile currentTile = tiles[4][4];  
		currentTile.setMap(map);
		currentTile.setLocation(new Location (4,4));
		e.setLocation(currentTile);
		int offensive = 4;
		
		Wall wall = new Wall();
		Floor floor = new Floor();
		
		Weapon weapon = new Weapon(offensive);
		weapon.setLocation(realTile);
		
		Decal decal = new Decal("red cross");
		InstantDeath instantDeath = new InstantDeath();
		
		realTile.setEntity(e);
		realTile.setItem(weapon);
		realTile.setTerrain(wall);
		realTile.setDecal(decal);
		realTile.setAreaEffect(instantDeath);
		
		Assert.assertEquals(wall.canPass(e), realTile.enter(e));
		
		realTile.setTerrain(floor);
		
		//false since entity is on the tile
		Assert.assertEquals(false, realTile.enter(e));
		
		
		realTile.setEntity(null);
		int lives = e.getLives();
		System.out.println(e.getLives());
		if(lives >= 1)
		{
			lives--;
		}
		boolean canEnter = realTile.enter(e);
		Assert.assertEquals(floor.canPass(e), canEnter);
		
		if(canEnter)
		{
			if(e.getInventory().getEquippableItems().isEmpty())
			{
				System.out.println("why!!!");
			}
			int start = 4;
			System.out.println("here");
			Assert.assertEquals(offensive+start, e.getInventory().getEquippableItems().get(0).applyOffenseBonus(start));
			Assert.assertEquals("red cross", realTile.getDecal().getTypeId());
			Assert.assertEquals(instantDeath, realTile.getAreaEffect());
			Assert.assertEquals(lives, e.getLives());
		}
		else
		{
			System.out.println("man");
		}
		
	}
	
	@After protected void tearDown() throws Exception 
	{ 
	super.tearDown();
	} 
}
